Last updated: 2024-05-04
Checks: 6 1
Knit directory: RA_SingleCellAnalysis/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
The R Markdown file has unstaged changes. To know which version of
the R Markdown file created these results, you’ll want to first commit
it to the Git repo. If you’re still working on the analysis, you can
ignore this warning. When you’re finished, you can run
wflow_publish to commit the R Markdown file and build the
HTML.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20240328) was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 2b701b2. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: data/cellbender_data_h5/
Ignored: output/._Cluster_Marker_Genes.xlsx
Ignored: output/00_sce_DataPreparation.rds
Ignored: output/01_sce_QualityControl.rds
Ignored: output/01_sce_QualityControl_filtered.rds
Ignored: output/02_sce_DimensionalityReduction.rds
Ignored: output/03_sce_Integration_Batchelor.rds
Ignored: output/03_sce_Integration_Harmony.rds
Ignored: output/04_sce_Clustering.rds
Ignored: output/05_sce_CelltypeAnnotation.rds
Ignored: output/06_sce_SC_Endothelial.rds
Ignored: output/06_sce_SC_Fibroblast.rds
Ignored: output/06_sce_SC_Myeloid.rds
Ignored: output/06_sce_SC_Tcell.rds
Ignored: output/08_DifferentialAbundance_miloR.rds
Ignored: output/Cluster_Marker_Genes.xlsx
Untracked files:
Untracked: analysis/01_QualityControl.Rmd
Unstaged changes:
Modified: analysis/00_DataPreparation.Rmd
Deleted: analysis/01_Preprocessing.Rmd
Modified: analysis/02_DimensionalityReduction.Rmd
Modified: analysis/03_Integration_Batchelor.Rmd
Modified: analysis/06_SC_Fibroblast.Rmd
Modified: analysis/_site.yml
Modified: analysis/index.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/00_DataPreparation.Rmd)
and HTML (docs/00_DataPreparation.html) files. If you’ve
configured a remote Git repository (see ?wflow_git_remote),
click on the hyperlinks in the table below to view the files as they
were in that past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | cd584b6 | sarloet | 2024-04-30 | fix |
| html | 3f073e5 | sarloet | 2024-04-30 | Build site. |
| Rmd | aa29156 | sarloet | 2024-04-30 | Fix |
| html | fc17296 | sarloet | 2024-04-23 | Build site. |
| Rmd | 6ac7e20 | sarloet | 2024-04-23 | Fix |
| Rmd | f73b2cd | sarloet | 2024-04-19 | fix |
| html | b1fc000 | sarloet | 2024-04-19 | Build site. |
| Rmd | 3f5d694 | sarloet | 2024-04-19 | Rerun |
| Rmd | c889cbe | sarloet | 2024-04-11 | fix |
| Rmd | 530ec4b | sarloet | 2024-04-05 | fix |
| Rmd | 64ae186 | sarloet | 2024-04-05 | fix |
| html | 5067fa6 | sarloet | 2024-04-04 | Build site. |
| html | fa0211e | sarloet | 2024-04-04 | Build site. |
| Rmd | d41ba14 | sarloet | 2024-04-03 | fix |
| html | bc77174 | sarloet | 2024-04-03 | Build site. |
| Rmd | 8543968 | sarloet | 2024-04-03 | fix |
| Rmd | 960ebd0 | sarloet | 2024-04-03 | fix |
| Rmd | eb121be | sarloet | 2024-04-03 | fix |
| html | ef4c495 | sarloet | 2024-04-02 | Build site. |
| Rmd | 9e3e00a | sarloet | 2024-04-02 | commit changes |
| Rmd | d291fd3 | sarloet | 2024-04-02 | initial commit |
| html | d291fd3 | sarloet | 2024-04-02 | initial commit |
library(here)
source(here("code", "standard_libraries.R"))
suppressPackageStartupMessages({
library(DropletUtils)
})
set.seed(123)
bpp <- BiocParallel::MulticoreParam(parallel::detectCores()-1, RNGseed=123)
path <- here::here()
View the CellRanger html report output for each sample
View the CellBender html report output for each sample
Load in the filtered feature barcode matrices from Cellbender of each sample and save them as sce object.
#Load Dataset
rawdata_folder <- paste0(path,"/data/cellbender_data_h5/")
#Get file names
filenames <- list.files(rawdata_folder ,recursive = F, full.names = F,pattern = "\\_filtered.h5$")
filepaths <- paste(rawdata_folder,filenames,sep = "")
#Load data as sce object
sce<-read10xCounts(samples = filepaths, sample.names = filenames,col.names=T,type="HDF5")
#Edit Filenames
colData(sce)$Sample = sub("\\_w_introns.*", "", sub("cellbender_output_", "", as.character(colData(sce)$Sample)))
sce
class: SingleCellExperiment
dim: 36601 119652
metadata(1): Samples
assays(1): counts
rownames(36601): ENSG00000243485 ENSG00000237613 ... ENSG00000278817
ENSG00000277196
rowData names(3): ID Symbol Type
colnames(119652): 1_AAGTACCGTCTCTCAC-1 1_GACTCTCAGGTAACTA-1 ...
17_AGGTGTTGTGGCATCC-1 17_GCCAGCACACAAGTGG-1
colData names(2): Sample Barcode
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):
#Metadata path
metadata_folder <- paste0(path,"/data/metadata/Metadata_Master.csv")
#Load metadata
metadata_df <-read.csv(metadata_folder, header=TRUE, sep=",")
metadata_df
#Add metadata to sce
colData(sce) <- dplyr::left_join(as.data.frame(colData(sce)),
metadata_df,
by= c("Sample" = "Sample"),
suffix=c(".x",".y")) %>%
#dplyr::select(Sample, Barcode, Diagnosis, sex, Age, Joint.Location, protocol, Pathotype,Krenn total score,) %>%
dplyr::select(-one_of("Comments")) %>% #select all except
DataFrame(row.names=colnames(sce))
#Set Sample names
names(colData(sce))[which(names(colData(sce))=="Sample")]="Orig.Identifier"
names(colData(sce))[which(names(colData(sce))=="ID")]="Sample"
#make row and col names unique
colnames(sce) <- paste0(sce$Sample, ".", sce$Barcode)
rownames(sce) <- paste0(rowData(sce)$ID, ".", rowData(sce)$Symbol)
#Dimensions of count matrix
dim(sce)
[1] 36601 119652
Dimensions of the count matrix
#Feautures/row data
data.frame(colData(sce))
#Droplet details / row data
data.frame(rowData(sce))
Show the number of cells detected in each sample or joint location before filtering
#Histogramm with number of cells per sample
ggplot(colData(sce), aes(x=Sample))+geom_bar()+ coord_flip()+ ggtitle("Number of cells per sample") + theme_bw()

data.frame(as.list(table(colData(sce)$Sample)))
#Histogramm with number of cells per sample
ggplot(colData(sce), aes(x=Joint.Location))+geom_bar()+ coord_flip()+ ggtitle("Number of cells per sample") + theme_bw()

data.frame(as.list(table(colData(sce)$Joint.Location)))
This plot shows cell counts per sample / count occurrence
#Number of genes detected per cell
#Total UMI for a gene versus the number of times detected
genesPerCell <- colSums(counts(sce) > 0)
plot(density(genesPerCell), xlab="Genes per cell", main="Number of genes detected per cell")

This plot gives an idea about the sequencing depth and if the sequencing has reached saturation or not. Plotted is the total gene count across all cells (x-axis) vs Proportion of cells the gene is detected in (y-axis) where each dot represents a gene.
#transcript_capture_efficiency
#Total UMI for a gene versus the number of times detected
tmpCounts <- counts(sce)
plot(rowSums(tmpCounts),
rowMeans(tmpCounts > 0),
log = "x",
xlab="total number of UMIs",
ylab="proportion of cells expressing the gene",
main="Total UMI for a gene vs times detected")

Initial filtering to remove unexpressed genes and cells with very low number of counts. The input to scDblFinder should not include empty droplets, and it might be necessary to remove cells with a very low coverage (e.g. <200 reads) to avoid errors. Further quality filtering should be performed downstream of doublet detection.
#Initial filtering before droplet removal
dim_before_filtering <- dim(sce)
#Get only the detected Genes
#Remove Cells with very low counts of less than 150 and genes not expressed
sce <- sce[rowSums(counts(sce)> 0) > 0, colSums(counts(sce)> 0) > 200]
dim_after_filtering <- dim(sce)
cat("NR of Cells Before Initial Filtering: ", dim_before_filtering[2],
"\nNR of Cells After Initial Filtering: ", dim_after_filtering[2],
"\nNR of Cells Filtered out: ", dim_before_filtering[2] - dim_after_filtering[2],
"\nCells Filtered out: [%]", (dim_before_filtering[2] - dim_after_filtering[2])/dim_before_filtering[2]*100,
"\n\nNR of Genes Before Initial Filtering: ", dim_before_filtering[1],
"\nNR of Genes After Initial Filtering: ", dim_after_filtering[1],
"\nNR of Genes Filtered out: ", dim_before_filtering[1] - dim_after_filtering[1],
"\nGenes Filtered out: [%]", (dim_before_filtering[1] - dim_after_filtering[1])/dim_before_filtering[1]*100
)
NR of Cells Before Initial Filtering: 119652
NR of Cells After Initial Filtering: 91083
NR of Cells Filtered out: 28569
Cells Filtered out: [%] 23.87674
NR of Genes Before Initial Filtering: 36601
NR of Genes After Initial Filtering: 34594
NR of Genes Filtered out: 2007
Genes Filtered out: [%] 5.483457
Doublets are defined as two cells that are sequenced under the same cellular barcode, which happens if they were captured in the same droplet. The scDblFinder method combines the strengths of various doublet detection approaches, training an iterative classifier on the neighborhood of real cells and artificial doublets.
#Detection
sce <- scDblFinder::scDblFinder(sce, samples=sce$Sample, BPPARAM = bpp)
table(sce$scDblFinder.class)
singlet doublet
82769 8314
as.data.frame.matrix(table(sce$Sample,sce$scDblFinder.class))
#Plot singlet/doublet histogramm Absolute comparison / by sample
as.data.frame(colData(sce)) %>%
dplyr::group_by(Sample, scDblFinder.class) %>%
dplyr::summarise(Freq=n()) %>%
ggplot(aes(x=Sample, y=Freq, fill=scDblFinder.class,label=Freq)) +
geom_bar(stat="identity") +
labs(title="Doublet detection results",
subtitle="By sample",x="",y="Number of Cells") +
geom_text(size=3, position = position_stack(vjust=0.5))+
theme(axis.text.x = element_text(angle = 45,hjust=1), axis.line = element_line(colour = "black"), panel.background = element_rect(fill = NA))

# Plot singlet/doublet histogramm Relative comparison / by sample
as.data.frame(colData(sce)) %>%
dplyr::group_by(Sample, scDblFinder.class) %>%
dplyr::summarise(Freq=n()) %>%
ggplot(aes(x=Sample, y=Freq, fill=scDblFinder.class, label=Freq)) +
geom_bar(stat="identity", position="fill") +
labs(title="Doublet detection results",
subtitle="By sample",
x="Sample",
y="Number of cells") +
geom_text(size=2.5, position = position_fill(vjust=0.5)) +
theme(axis.text.x = element_text(angle = 45,hjust=1), axis.line = element_line(colour = "black"), panel.background = element_rect(fill = NA))

sce <- addPerCellQC(sce)
#Plot singlet/doublet qcplots
colData(sce) %>%
as.data.frame() %>%
arrange(scDblFinder.class) %>%
ggplot(aes(x = sce$sum, y = sce$detected )) +
geom_point(aes(colour = scDblFinder.class)) +
facet_wrap(vars(Sample))+
labs(title="Total number of detected genes plotted against total number of UMIs",
x="Total counts",
y="Detected genes") +
theme(strip.background=element_rect(fill="white"), panel.background = element_rect(fill = NA),axis.line = element_line(colour = "black"))

print(paste0("NR of Cells Before Doublet Removal ", dim(sce)[2]))
[1] "NR of Cells Before Doublet Removal 91083"
#Apply Doublet Removal
sce <- sce[ ,sce$scDblFinder.class == "singlet"]
print(paste0("NR of Cells After Doublet Removal ", dim(sce)[2]))
[1] "NR of Cells After Doublet Removal 82769"
saveRDS(sce, file =paste0(path,'/output/00_sce_DataPreparation.rds'))
sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Warsaw
tzcode source: internal
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] DropletUtils_1.22.0 gridExtra_2.3
[3] scran_1.30.2 scater_1.30.1
[5] scuttle_1.12.0 SingleCellExperiment_1.24.0
[7] SummarizedExperiment_1.32.0 Biobase_2.62.0
[9] GenomicRanges_1.54.1 GenomeInfoDb_1.38.8
[11] IRanges_2.36.0 S4Vectors_0.40.2
[13] BiocGenerics_0.48.1 MatrixGenerics_1.14.0
[15] matrixStats_1.3.0 dplyr_1.1.4
[17] ggplot2_3.5.0 BiocParallel_1.36.0
[19] here_1.0.1 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] rstudioapi_0.16.0 jsonlite_1.8.8
[3] magrittr_2.0.3 ggbeeswarm_0.7.2
[5] farver_2.1.1 rmarkdown_2.26
[7] fs_1.6.3 BiocIO_1.12.0
[9] zlibbioc_1.48.2 vctrs_0.6.5
[11] Rsamtools_2.18.0 DelayedMatrixStats_1.24.0
[13] RCurl_1.98-1.14 htmltools_0.5.8.1
[15] S4Arrays_1.2.1 BiocNeighbors_1.20.2
[17] xgboost_1.7.7.1 Rhdf5lib_1.24.2
[19] SparseArray_1.2.4 rhdf5_2.46.1
[21] sass_0.4.9 bslib_0.7.0
[23] cachem_1.0.8 GenomicAlignments_1.38.2
[25] whisker_0.4.1 igraph_2.0.3
[27] lifecycle_1.0.4 pkgconfig_2.0.3
[29] rsvd_1.0.5 Matrix_1.6-5
[31] R6_2.5.1 fastmap_1.1.1
[33] GenomeInfoDbData_1.2.11 digest_0.6.35
[35] colorspace_2.1-0 ps_1.7.6
[37] rprojroot_2.0.4 dqrng_0.3.2
[39] irlba_2.3.5.1 beachmat_2.18.1
[41] labeling_0.4.3 fansi_1.0.6
[43] httr_1.4.7 abind_1.4-5
[45] compiler_4.3.3 withr_3.0.0
[47] viridis_0.6.5 highr_0.10
[49] HDF5Array_1.30.1 R.utils_2.12.3
[51] MASS_7.3-60.0.1 DelayedArray_0.28.0
[53] rjson_0.2.21 bluster_1.12.0
[55] tools_4.3.3 vipor_0.4.7
[57] beeswarm_0.4.0 httpuv_1.6.15
[59] R.oo_1.26.0 glue_1.7.0
[61] restfulr_0.0.15 callr_3.7.6
[63] rhdf5filters_1.14.1 promises_1.3.0
[65] grid_4.3.3 getPass_0.2-4
[67] cluster_2.1.6 generics_0.1.3
[69] gtable_0.3.4 R.methodsS3_1.8.2
[71] data.table_1.15.4 BiocSingular_1.18.0
[73] ScaledMatrix_1.10.0 metapod_1.10.1
[75] utf8_1.2.4 XVector_0.42.0
[77] ggrepel_0.9.5 pillar_1.9.0
[79] stringr_1.5.1 limma_3.58.1
[81] later_1.3.2 lattice_0.22-5
[83] rtracklayer_1.62.0 tidyselect_1.2.1
[85] locfit_1.5-9.9 Biostrings_2.70.3
[87] knitr_1.45 git2r_0.33.0
[89] edgeR_4.0.16 xfun_0.43
[91] statmod_1.5.0 stringi_1.8.3
[93] yaml_2.3.8 evaluate_0.23
[95] codetools_0.2-19 tibble_3.2.1
[97] cli_3.6.2 munsell_0.5.1
[99] processx_3.8.4 jquerylib_0.1.4
[101] Rcpp_1.0.12 XML_3.99-0.16.1
[103] parallel_4.3.3 sparseMatrixStats_1.14.0
[105] bitops_1.0-7 viridisLite_0.4.2
[107] scales_1.3.0 scDblFinder_1.16.0
[109] crayon_1.5.2 rlang_1.1.3